package com.duapk.index;

import java.util.Date;
import java.util.Timer;

import com.duapk.index.cache.ResourceCache;
import com.duapk.index.conf.ConfigFactory;
import com.duapk.index.conf.LogConfig;
import com.duapk.index.task.IndexTask;
import com.duapk.index.util.StringTools;
import com.duapk.index.work.ShutdownServer;
import com.duapk.nlp.constant.ChineseConstants;

public class Start {
	public static void main(String[] args) {
		if (args.length < 3) {
			System.out
					.println("Please usage: java Start logback.xml config.xml port");
			System.exit(1);
		}

		// 配置文件加载及缓存加载
		ConfigFactory.init(args[1]);
		LogConfig.config(args[0]);
		if (System.getProperty(ChineseConstants.CHINESE_WORD_PATH) == null) {
			System.setProperty(ChineseConstants.CHINESE_WORD_PATH,
					ConfigFactory.getString("Path.modelsDir"));
		}
		// 监听端口
		new Thread(new ShutdownServer(Integer.parseInt(args[2]))).start();

		ResourceCache.getInstance();

		startTask();
	}

	/**
	 * 开启任务
	 * 
	 * @param createOnly
	 *            只开启全量索引更新的标识
	 */
	private static void startTask() {
		Date startTime = StringTools.getStartTime(ConfigFactory.getString(
				"IndexTask.startTime", "00:30"), "00:30");
		long freq = ConfigFactory.getInt("IndexTask.frequency", 24) * 3600L * 1000;
		System.out
				.println("apk_index start. the first time to create index is "
						+ startTime);
		// 开启全量索引创建任务
		Timer timer = new Timer();
		timer.schedule(new IndexTask(), startTime, freq);
	}
}
